# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=benchmark
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_gtest_ver=1.8.1
pkgver=1.5.0
pkgrel=1
pkgdesc="A microbenchmark support library, by Google (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/google/benchmark"
license=('APACHE')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gtest")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/google/benchmark/archive/v${pkgver}.tar.gz"
        googletest-release-${_gtest_ver}.tar.gz::https://github.com/google/googletest/archive/release-${_gtest_ver}.tar.gz
        "benchmark-mingw.patch")
sha256sums=('3c6a165b6ecc948967a1ead710d4a181d7b0fbcaa183ef7ea84604994966221a'
            '9bf1fe5182a604b4135edc1a425ae356c9ad15e9b23f9f12a02e80184c3a249c'
            '00a34d29f1dc4339513c4ebabd00560b446624a3380edc98a57c0c43484473f7')

prepare() {
  cp -r googletest-release-${_gtest_ver} ${_realname}-${pkgver}/googletest

  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/benchmark-mingw.patch"
  sed -i "s|v0.0.0|v${pkgver}|g" "cmake/GetGitVersion.cmake"
}

build() {
  # Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}"/build-${MINGW_CHOST}-shared && cd "${srcdir}"/build-${MINGW_CHOST}-shared
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DBUILD_SHARED_LIBS="ON" \
      ../${_realname}-${pkgver}
      
  make
  
  # Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}"/build-${MINGW_CHOST}-static && cd "${srcdir}"/build-${MINGW_CHOST}-static
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DBUILD_SHARED_LIBS="OFF" \
      ../${_realname}-${pkgver}
      
  make
}

check() {
  # Shared Check
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  make test || echo "Ignore for now, as the test is expected to fail: https://github.com/google/benchmark/issues/578"
  
  # Static Check
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  make test || echo "Ignore for now, as the test is expected to fail: https://github.com/google/benchmark/issues/578"
}

package() {
  # Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  make DESTDIR="${pkgdir}" install
  
  # Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  make DESTDIR="${pkgdir}" install
  
  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
